package ioc

import (
	"github.com/gin-gonic/gin"
	redis2 "github.com/redis/go-redis/v9"
	"time"
	"web-server/client/mysql"
	"web-server/client/redis"
	"web-server/dao"
	"web-server/middleware"
	"web-server/middleware/ratelimit"
)

func InitMiddleware(rds redis2.Cmdable) []gin.HandlerFunc {
	return []gin.HandlerFunc{
		middleware.NewLoginJWTMiddlewareBuilder().IncludePaths([]string{"/limit"}).Build(),
		ratelimit.NewBuilder(rds, time.Minute, 5).Build(),
	}
}

func InitServer() *gin.Engine {
	mysqlDB := mysql.GetDB()
	redisDB := redis.GetDB()
	mdls := InitMiddleware(redisDB)
	dao.NewUserDao(mysqlDB)
	server := gin.Default()
	server.Use(mdls...)
	return server
}
